﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCCommon;
using BCCommon.Enums;
using BCData.WeChatComponent.WeChatMediaRecord;
using BCData.WeChatComponent.WeChatMessageRecord;
using BCDto.WeChatComponent.WeChatCustom;
using BCDto.WeChatComponent.WeChatMediaRecord;
using BCEntity.WeChatComponent.WeChatMediaRecord;
using BCEntity.WeChatComponent.WeChatMessageRecord;
using BCService.Utils;
using Microsoft.Extensions.Configuration;
using WeChatSDK.WeChatDto.CustomSerivce.Message;
using WeChatSDK.WeChatServiceComponent.CustomService.KeyAccount;

namespace BCService.WeChatComponent.WeChatCustomService
{
    /// <summary>
    /// 微信客服服务
    /// </summary>
    public class WeChatCustomService : IWeChatCustomService
    {
        private readonly IConfiguration configuration;
        private readonly IWeChatMessageRecordData weChatMessageRecordData;
        private readonly IWeChatMediaRecordData weChatMediaRecordData;
        private readonly KeyAccountService keyAccountService;
        private readonly string weChatOriginalId;
        private readonly string weChatName;
        public WeChatCustomService(IConfiguration configuration,
            IWeChatMessageRecordData weChatMessageRecordData,
            IWeChatMediaRecordData weChatMediaRecordData)
        {
            this.configuration = configuration;
            this.weChatMessageRecordData = weChatMessageRecordData;
            this.weChatMediaRecordData = weChatMediaRecordData;
            keyAccountService = new KeyAccountService();
            this.weChatOriginalId = configuration["WeChatService:Account"];
            this.weChatName = configuration["WeChatService:WeChatName"];
        }

        public void SendMessage(WeChatCustomMessageRequestDto weChatCustomMessageRequestDto)
        {
            WeChatKeyAccountMessageDto weChatKeyAccountMessageDto;
            WeChatMediaRecordEntity weChatMediaRecordEntity = default(WeChatMediaRecordEntity);
            if (weChatCustomMessageRequestDto.WeChatMessageType != WeChatMessageType.Text && !string.IsNullOrWhiteSpace(weChatCustomMessageRequestDto.MediaId))
            {
                weChatMediaRecordEntity = this.weChatMediaRecordData.GetEntity(weChatCustomMessageRequestDto.MediaId);
            }
            switch (weChatCustomMessageRequestDto.WeChatMessageType)
            {
                case WeChatMessageType.Text:
                    weChatKeyAccountMessageDto = new TextDto()
                    {
                        Msgtype = "text",
                        Text = new TextContentDto()
                        {
                            Content = weChatCustomMessageRequestDto.Content,
                        },
                        Touser = weChatCustomMessageRequestDto.ToUserOpenid
                    };
                    break;
                case WeChatMessageType.Image:
                    weChatKeyAccountMessageDto = new ImageDto()
                    {
                        Msgtype = "image",
                        Image = new WeChatSDK.WeChatDto.MP.MPMediaDto()
                        {
                            MediaId = weChatCustomMessageRequestDto.MediaId
                        },
                        Touser = weChatCustomMessageRequestDto.ToUserOpenid
                    };
                    break;
                case WeChatMessageType.Voice:
                    weChatKeyAccountMessageDto = new VoiceDto()
                    {
                        Msgtype = "voice",
                        Voice = new MediaMaterialDto()
                        {
                            MediaId = weChatCustomMessageRequestDto.MediaId
                        },
                        Touser = weChatCustomMessageRequestDto.ToUserOpenid
                    };
                    break;
                case WeChatMessageType.Video:
                    weChatKeyAccountMessageDto = new VideoDto()
                    {
                        Msgtype = "video",
                        Video = new KeyAccountVideoMediaDto()
                        {
                            Description = weChatMediaRecordEntity.Description,
                            ThumbMediaId = weChatCustomMessageRequestDto.ThumbMediaId,
                            Title = weChatMediaRecordEntity.Title,
                            MediaId = weChatCustomMessageRequestDto.MediaId
                        },
                        Touser = weChatCustomMessageRequestDto.ToUserOpenid
                    };
                    break;
                case WeChatMessageType.Music:
                    weChatKeyAccountMessageDto = new MusicDto()
                    {
                        Msgtype = "music",
                        Music = new KeyAccountMusicMediaDto()
                        {
                            Description = weChatCustomMessageRequestDto.Description,
                            ThumbMediaId = weChatCustomMessageRequestDto.ThumbMediaId,
                            Title = weChatCustomMessageRequestDto.Title,
                            Musicurl = weChatMediaRecordEntity.LocalUrl,
                            Hqmusicurl = weChatMediaRecordEntity.LocalUrl,
                            MediaId = weChatCustomMessageRequestDto.MediaId
                        },
                        Touser = weChatCustomMessageRequestDto.ToUserOpenid
                    };
                    break;
                case WeChatMessageType.News:
                    if (weChatCustomMessageRequestDto.Articles!=null && weChatCustomMessageRequestDto.Articles.Any())
                    {
                        weChatKeyAccountMessageDto = new NewsDto()
                        {
                            Msgtype = "news",
                            News = new NewsSubDto()
                            {
                                Articles = weChatCustomMessageRequestDto.Articles.As<IEnumerable<ArticlesDto>>().ToArray()
                            },
                            Touser = weChatCustomMessageRequestDto.ToUserOpenid
                        };
                    }
                    else
                    {
                        weChatKeyAccountMessageDto = new MPNewsDto()
                        {
                            Msgtype = "mpnews",
                            Mpnews = new WeChatSDK.WeChatDto.MP.MPMediaDto()
                            {
                                MediaId = weChatCustomMessageRequestDto.MediaId
                            },
                            Touser = weChatCustomMessageRequestDto.ToUserOpenid
                        };
                    }
                    break;
                default:
                    throw new ArgumentException("无法识别的客服回复消息类型！");
            }
            
            string result = WeChatHttpResponseHandle.ProcessMessage(keyAccountService.SendMessage(weChatKeyAccountMessageDto));
            WeChatMessageRecordEntity weChatMessageRecordEntity = new WeChatMessageRecordEntity()
            {
                FromUserName = this.weChatOriginalId,
                ToUserName = weChatCustomMessageRequestDto.ToUserOpenid,
                MessageType = weChatCustomMessageRequestDto.WeChatMessageType,
                Content = weChatCustomMessageRequestDto.Content,
                MessageId = weChatCustomMessageRequestDto.MessageId,
                MediaId = weChatCustomMessageRequestDto.MediaId,
                ThumbMediaId = weChatCustomMessageRequestDto.ThumbMediaId,
                Title = weChatCustomMessageRequestDto.Title,
                Description = weChatCustomMessageRequestDto.Description,
                IsReply = true
            };
            if (weChatMediaRecordEntity != default(WeChatMediaRecordEntity))
            {
                weChatMessageRecordEntity.Title = weChatMediaRecordEntity.Title;
                weChatMessageRecordEntity.Description = weChatMediaRecordEntity.Description;
                weChatMessageRecordEntity.Url = weChatMediaRecordEntity.MusicUrl;
                weChatMessageRecordEntity.Url = weChatMediaRecordEntity.HQMusicUrl;
            }

            this.weChatMessageRecordData.Add(weChatMessageRecordEntity);
            this.weChatMessageRecordData.UpdateRepliedStatus(weChatCustomMessageRequestDto.MessageId);
        }
    }
}
